import os

class OrderManager(object):
    channels = {}
    triggers = ["!order","!orders","!addorder","!addorders","!delorder","!delorders",
                "!aid","!addaid","!delaid"]
    name = "orders"
    help = ["Channel orders commands:",
            "%c6!orders%c"%(03,03),
            "%c6!addorder%c <order>"%(03,03),
            "%c6!delorder%c <order#>"%(03,03)]

    def command(self, firstword, user, channel, msg):
        if not self.channels.has_key(channel):
            self.channels[channel] = ChannelOrders(channel)
        if msg.lower() in ["!order","!orders"]:
            return self.channels[channel].printOrders()
        if msg.lower() == "!aid":
            return self.channels[channel].printAid()
        if firstword == '!addorder':
            order = msg[10:]
            return self.channels[channel].addOrder(order, user)
        if firstword == '!addaid':
            request = msg[8:]
            return self.channels[channel].addAid(request, user)
        if firstword == '!delorder':
            try: number = int(msg[10:])
            except: return {'chanmsg': ["<%s> is not a valid number."%msg[10:]]}
            return self.channels[channel].delOrder(number)
        if firstword == '!delaid':
            try: number = int(msg[8:])
            except: return {'chanmsg': ["<%s> is not a valid number."%msg[10:]]}
            return self.channels[channel].delAid(number)
        return {}


class ChannelOrders(object):
    def __init__(self, channel):
        self.orders = []
        self.aid = []
        self.filename = ".\\" + channel + "_orders.txt"
        if os.path.exists(self.filename):
            f = open(self.filename, 'r')
            lines = f.read().split('\n')
            f.close()

            i = lines.index('')
            self.orders = lines[:i]
            self.aid = lines[i+1:]
            if '' in self.aid: self.aid.remove('')
    
    def save(self):
        f = open(self.filename, 'w')
        f.writelines(map(lambda i: self.orders[i]+'\n', range(len(self.orders))))
        f.write('\n')
        f.writelines(map(lambda i: self.aid[i]+'\n', range(len(self.aid)-1)))
        if len(self.aid): f.write(self.aid[-1])
        f.close()
    
    def addAid(self, aid, person):
        if len(self.aid) >= 10: return {'chanmsg':["Maximum amount of aid requests reached."]}
        aid_str = aid + " - " + person
        self.aid.append(aid_str)
        self.save()
        return {'chanmsg':["Added aid request: " + aid_str]}
    
    def delAid(self, number):
        if number > len(self.aid): return {'chanmsg': ["No aid request number %d." % number]}
        if number <= 0: return {'chanmsg': ["Invalid aid request number, dumbass."]}
        removed = self.aid.pop(number-1)
        self.save()
        return {'chanmsg':["Removed request: " + removed]}
    
    def printAid(self):
        if len(self.aid) == 0: return {'chanmsg':["At the moment there are no aid requests."]}
        print_aid = map(lambda i: "%c3 %d%c: "%(03,i+1,03) + self.aid[i], range(len(self.aid)))
        return {'chanmsg':print_aid}
    
    def addOrder(self, order, person):
        if len(self.orders) >= 10: return {'chanmsg':["Maximum amount of orders."]}
        order_str = order + " - " + person
        self.orders.append(order_str)
        self.save()
        return {'chanmsg':["Added order: " + order_str]}
    
    def delOrder(self, number):
        if number > len(self.orders): return {'chanmsg': ["No ordernumber %d." % number]}
        if number <= 0: return {'chanmsg': ["Invalid order number, dumbass."]}
        removed = self.orders.pop(number-1)
        self.save()
        return {'chanmsg':["Removed order: " + removed]}
        
    def printOrders(self):
        if len(self.orders) == 0: return {'chanmsg':["At the moment there are no orders."]}
        print_orders = map(lambda i: "%c4 %d%c: %c"%(03,i+1,03,02) + self.orders[i], range(len(self.orders)))
        if len(self.aid): print_orders.append("There are also%c10 %d %c6!aid%c requests waiting."%(03,len(self.aid),03,03))
        return {'chanmsg':print_orders}